package cn.edu.sjtu.se.dslab.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import cn.edu.sjtu.se.dslab.action.bean.PageBean;
import cn.edu.sjtu.se.dslab.component.Component;
import cn.edu.sjtu.se.dslab.component.method.MethodComponent;
import cn.edu.sjtu.se.dslab.dao.IBaseDao;
import cn.edu.sjtu.se.dslab.dao.IComponentDao;
import cn.edu.sjtu.se.dslab.dao.IMethodComponentDao;
import cn.edu.sjtu.se.dslab.exception.DBException;
import cn.edu.sjtu.se.dslab.util.Const;
import cn.edu.sjtu.se.dslab.util.MD5;

@Repository("methodComponentDao")
public class MethodComponentDaoImpl implements IMethodComponentDao {

	@Resource(name = "baseDao")
	private IBaseDao<MethodComponent> baseDao;

	@Resource(name = "componentDao")
	private IComponentDao componentDao;

	private static final String DELETED = "deleted";
	
	@Override
	public void save(MethodComponent component) {
		// TODO Auto-generated method stub

		String temp = component.getDescription() + 
				+  System.currentTimeMillis();
		MD5 md5 = new MD5();
		String uri = md5.getMD5ofStr(temp).substring(0, 8);
		while (!componentDao.checkUri(Const.URI_METHOD_COMPONENT + ":" +uri)) {
			temp =  component.getDescription() + 
					+  System.currentTimeMillis();
			uri = md5.getMD5ofStr(temp).substring(0, 8);
		}
		component.setUri(Const.URI_METHOD_COMPONENT + ":" + uri);
		baseDao.save(component);
	}

	@Override
	public void update(MethodComponent component) {
		// TODO Auto-generated method stub
		baseDao.update(component);
	}

	@Override
	public MethodComponent queryById(long id) {
		// TODO Auto-generated method stub
		return baseDao.queryById(MethodComponent.class, id);
	}

	@Override
	public List<MethodComponent> queryAll() {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from MethodComponent a where ");
		hql.append(" a.project=null and ");
		hql.append(DELETED + "=0");
		return baseDao.query(hql.toString());
	}

	@Override
	public PageBean<MethodComponent> queryByPageNum(int pageNo)
			throws DBException {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from MethodComponent a where ");
		hql.append(" a.project=null and ");
		hql.append(DELETED + "=0");
		PageBean<MethodComponent> bean = baseDao.queryByspecialHqlForPage(pageNo,
				Const.PAGESIZE, hql.toString());
		return bean;
	}

	@Override
	public MethodComponent queryByUri(String uri) {
		// TODO Auto-generated method stub
		Component component = componentDao.queryByUri(uri);
		if (component != null) {
			return baseDao.queryById(MethodComponent.class, component.getId());
		}
		return null;
	}

	@Override
	public List<MethodComponent> queryByName(String name) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from MethodComponent a where ");
		hql.append(" a.name like '%");
		hql.append( name+ "%'");
		hql.append(" and a.project=null and ");
		hql.append(DELETED + "=0");
		return baseDao.query(hql.toString());
	}

	@Override
	public List<MethodComponent> searchComponentByText(String text) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from MethodComponent a where ");
		hql.append(" a.name like '%");
		hql.append( text+ "%' or ");
		hql.append(" a.description like '%");
		hql.append( text+ "%' ");
		hql.append(" and a.project=null and ");
		hql.append(DELETED + "=0");
		return baseDao.query(hql.toString());
	}

}
